<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <!-- 搜索框区域 -->
      <el-row type="flex" justify="" :gutter="20">
        <!-- 搜索框 -->
        <el-col :span="7">
          <div>
            <el-input
              placeholder="请输入搜索内容"
              v-model="queryInfo.query"
              clearable
              @clear="getOrderList()"
              class="input-with-select"
            >
              <!-- 搜索按钮 -->
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="getOrderList()"
              ></el-button>
            </el-input>
          </div>
        </el-col>
      </el-row>
      <!-- 订单列表表格区域 -->
      <el-table :data="orderList" stripe border="" style="width: 100%">
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="450">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格" align="center">
        </el-table-column>
        <!-- 付款状态 -->
        <el-table-column prop="pay_status" label="是否付款" align="center">
          <template slot-scope="scope">
            <div>
              <el-tag type="danger" v-if="scope.row.pay_status == 0"
                >未付款</el-tag
              >
              <el-tag type="success" v-else>已付款</el-tag>
            </div>
          </template>
        </el-table-column>
        <!-- 是否发货 -->
        <el-table-column prop="trade_no" label="是否发货" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.trade_no == ''">否</span>
            <span v-else>是</span>
          </template>
        </el-table-column>
        <el-table-column label="下单时间" width="170" align="center">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <!-- 操作列 -->
        <el-table-column label="操作" width="120" align="center">
          <template slot-scope="scope">
            <div>
              <!-- 修改收获地址 -->
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                circle
                @click="toEditTheAddress(scope.row)"
              ></el-button>

              <el-button
                type="success"
                icon="el-icon-location"
                size="mini"
                circle
                @click="showLocation(scope.row)"
              ></el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 商品分页模块 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改收货地址的对话框 -->
    <el-dialog
      title="修改收货地址"
      :visible.sync="editAddressVisible"
      width="50%"
      @close="handleEditAddressDialogClose"
    >
      <el-form
        :model="editAddressForm"
        :rules="editAddressFormRules"
        ref="editAddressFormRef"
        label-width="100px"
      >
        <el-form-item label="区域" prop="generalAddress">
          <el-cascader
            v-model="editAddressForm.generalAddress"
            :options="citydata"
            :props="{ expandTrigger: 'hover' }"
            @change="handleGeneralAddressChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="detailAddress">
          <el-input v-model="editAddressForm.detailAddress"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editAddressVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAddressVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 展示当前订单物流对话框 -->
    <el-dialog
      title="当前物流"
      :visible.sync="LoactionDialogVisible"
      width="50%"
    >
      <!-- 利用timeline组件展示物流信息 -->
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in nowLocation"
          :key="index"
          :timestamp="activity.times"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="LoactionDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="LoactionDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
//引入 中国省市区数据 可以直接作为级联选择器的数据来源
import citydata from "./citydata.js";
export default {
  data() {
    return {
      // 订单列表数组
      orderList: [],
      //获取订单列表的查询参数
      queryInfo: {
        query: "", //查询参数  可以为空
        pagenum: 1, //当前页码  不能为空
        pagesize: 10, //每页显示条数  不能为空
        user_id: "", //用户 ID可以为空
        pay_status: "", //支付状态  可以为空
        is_send: "", //是否发货  可以为空
        order_fapiao_title: "", //['个人','公司']  可以为空
        order_fapiao_company: "", //公司名称  可以为空
        order_fapiao_content: "", //发票内容  可以为空
        consignee_addr: "", //发货地址  可以为空
      },
      //总共有多少条订单数据
      total: 0,
      //控制修改收货地址对话框是否显示的变量 默认为false
      editAddressVisible: false,
      //修改收货地址表单
      editAddressForm: {
        generalAddress: "",
        detailAddress: "",
      },
      //修改收货地址表单验证规则
      editAddressFormRules: {
        generalAddress: [
          { required: true, message: "请选择区域", trigger: "blur" },
        ],
        detailAddress: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
      },
      //中国省市数据 级联选择器使用
      citydata: citydata,
      //控制展示物流信息对话框是否显示的变量 默认为false
      LoactionDialogVisible: false,
      //存放当前操作订单的物流信息
      nowLocation: [],
    };
  },
  methods: {
    //获取订单列表的函数
    async getOrderList() {
      const { data: res } = await this.$http.get(`orders`, {
        params: this.queryInfo,
      });
      console.log(res);
      if (res.meta.status == 200) {
        this.orderList = res.data.goods;
        this.total = res.data.total;
      } else {
        this.$message.error(`获取订单列表失败${res.meta.msg}`);
      }
    },
    //分页器模块的相关功能
    handleSizeChange(val) {
      //处理每页显示条数的改变
      this.queryInfo.pagesize = val;
      this.getOrderList();
    },
    handleCurrentChange(val) {
      //处理当前页改变
      this.queryInfo.pagenum = val;
      this.getOrderList();
    },
    //修改收获地址
    toEditTheAddress(val) {
      //显示对话框
      this.editAddressVisible = true;
    },
    //修改省市级联选择器 被更改时候触发的函数
    handleGeneralAddressChange() {},
    //编辑地址对话框关闭 触发的函数
    handleEditAddressDialogClose() {
      //重置表单
      this.$refs.editAddressFormRef.resetFields();
    },
    //展示物流信息按钮点击后 触发的函数
    async showLocation(val) {
      //将显示物流的对话框显示出来
      this.LoactionDialogVisible = true;
      // 因为快递100的接口改了  查询物流信息宣告破产
      //获取物流信息 测试用单号1106975712662
      const { data: res } = await this.$http.get(`kuaidi/1106975712662`);
      if (res.meta.status == 200) {
        console.log(res.data);
        this.nowLocation = res.data;
      } else {
        this.$message.error("未能成功获取物流信息");
      }
    },
  },
  created() {
    //页面一创建就获取订单列表
    this.getOrderList();
  },
};
</script>

<style lang="less" scoped>
</style>